import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import Home from '../components/Home.vue'

const router = createRouter({
  linkActiveClass:'router-active',
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
      {
        path: '/',
        redirect:'/home'
      },
      {
        path: '/home',
        name: 'home',
        component: Home
      },
      {
        path: '/about',
        name: 'about',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.懒加载
        component: () => import('../components/About.vue'),
        children:[
          {path:'tab1',component: () => import('../pages/Tab1.vue')},
          {path:'tab2',component: () => import('../pages/Tab2.vue')},
        ]
      },
      {
        path:'/movie',
        name:'movie',
        component:() => import('../components/Movie.vue'),
        children:[
          {path:':userId',component:() => import('../components/MovieDetails.vue'),name:'MovieDetails' , props:true}
        ]
      },
      {
        path: '/:error*',
        name: 'notFound',
        //notFound404页面用来兜底的，一定要放在路由规则的最下面
        component: () => import('../views/notFound.vue')
      }
    ]
  })

  
  // const  router =useRouter()
  router.beforeEach((to, from,next) => {
    // console.log(to, from);
    // ...
    // 返回 false 以取消导航
    // return false
    let isLogin=window.sessionStorage.getItem('userInfo')
    if (isLogin) {
      next()
    } else {
      next()

    }
   
  })
  export default router